const {
	Backend,
	Joi
} = require('dx-router-admin');
const _ = require('underscore');
module.exports = class Job extends Backend {
	constructor(ctx) {
		super(ctx);
		//this.multiFields = ['enable', 'is_link'];
		// this.dto = {
		// 	menu_id: Joi.string().required().error(new Error('菜单标识不能为空')),
		// 	name: Joi.string().required().error(new Error('菜单名称不能为空')),
		// 	enable: Joi.boolean().required().error(new Error('状态类型不正确')),
		// };
	}
	async index(param){
		if(param.company_name||param.company_ids){
			const clist = await this.$dx.service.company.findCompanyIds(param);
			param.company_id = ['in',_.pluck(clist,'_id')]
			delete param.company_name;
		}
		//const t1 =  new Date().getTime();
		//this.indexWith = ['hrobj','company'];
		const response = await super.index(param);
		const list = response.data.list;
		const _ids = _.uniq(_.pluck(list,'company_id'));
		const companys = await this.$dx.service.company.findByIds(_ids);
		// // /*   查找 HR */
		const _hids = _.pluck(list,'hr_id');
		const hrusers = await this.$dx.service.talent.findByIds(_hids);
		for(let i=0;i<list.length;i++){
			const company = _.findWhere(companys,{_id:list[i].company_id});
			if(company){
				list[i].company_name = company.name;
				list[i].company_contract = company.contract;
				list[i].company_email = company.email;
				list[i].hrlist = company.hrlist;
			}
			const hr = _.findWhere(hrusers,{_id:list[i].hr_id});
			list[i].hrobj = hr;
		}
		return response;
	}
	get(param){
		if(param._cid){
			return this.currentService.with(['hr','company']).where({company_id:param._cid}).limit(100).select();
		}
	}
	async check(param){
		const timestamp = new Date().getTime() - 90*24*3600*1000;
		this.currentService.where({
			create_date:['lt',timestamp],
			editor_id:['eq','']
		}).delete();
	}
	
};